clear all;close all;clc

% % 63, 64, 68, 69, 70, 72, 73, 74, 75, 76, 77, 78
% Subjects = {'A63','A64','A68','A69','A70','A72','A73','A74','A75','A76','A77','A78','A79','A80','A81','A82'};

Sub = '79';
Session = 8;

fingers = 'abcde';  
finger_names = {'Thumb';'Index';'Middle';'Ring';'Pinky'};
f_low = 0.1;
f_high = 30;
FS = 198.5;
% FS = 198.5;
% FS = 200;
dt = 1/FS;
[b,a]=butter(3,[f_low f_high]./(FS./2));
% FS_low = 25;
% Q = FS./FS_low;
f_Conditions_EMG_EEG_up
f1 = figure;
f2 = figure;

%--------compute rms-------
% EMG_rms = 0;
%--------------------------

for ifinger = 1:5
    ifinger
    filename = ['EMG_data\' Sub '_' num2str(Session) fingers(ifinger) '.csv'] %read or import
    eval(['EMG8_old = csvread( filename);']);%read or import EMG data, and make it into rows
    t0 = EMG8_old(:,1);
    
    t = t0(1);
    EMG8 = EMG8_old(1,:);
    for i = 2:length(t0)
        if t0(i) > t(end)
            t = [t t0(i)];
            EMG8 = [
                EMG8
                EMG8_old(i,:)];
        end
    end
     %-------------------
    if  max(t) >= 1000
        t = t/1000;
    end
        
        %-------------------
%     [t, Inewt] = sort(t0);
%     EMG8=EMG8_old(Inewt,:);
    
    %     diff(t)
    %     sdf0
    %     for itt = 2:length(t)
    %         if t(itt)-t(itt-1) < 0
    %             t(itt) = t(itt)+212;
    %         end
    %     end
    %-------------------
    Num_muscles = 6 ;
    % first, plot the original 8 channel EMG:
    figure(f1);
    subplot(2,3,ifinger)
    % EEG triggers:
    All_sess=[];
    eval(['condall = A' Sub '_all;']);
    for i = 1:length(condall)
         if length(condall{i})==length( num2str(Session))+1
            if condall{i}==[num2str(Session) fingers(ifinger)];
                EEG_name = ['EEG_data\' condall{i+1}]
                shift = condall{i+2}(3)
                chunkline = condall{i+2};        
            end
         
         end
    end
    sample1 = 1;
    sample2 = 10;
    eeg = read_eep_cnt(EEG_name, sample1, sample2);
    sample2=eeg.nsample;
    eeg = read_eep_cnt(EEG_name, sample1, sample2);
    outTT = f_process_trigger(eeg,chunkline,0,20);
    newTR_time = outTT.newTR_time-shift;
    
    
    for i=2:9
        EMG_new(:,i) = filter(b,a,EMG8(:,i));
        %         t2 =
        plot(t,EMG_new(:,i)./15+i-1);
        %         plot(EMG_new(:,i)+i.*20);
        hold on
    end
    xlabel('Time (s)');
    %     xlim([0 t(end)]);
    ylabel('Channel');
    ylim([0 9])
    title(finger_names{ifinger})

    EMG_inter = EMG_new(:,2:9)';
    
    for itr = 1:length(newTR_time)
        plot(newTR_time(itr).*[1 1],[0 200],'r');
        for ichannel = 1:8
            [Yt, p1 ] =min(abs(t-newTR_time(itr)));
            [Yt, p2 ] =min(abs(t-newTR_time(itr)-1));
            
            
            [Yt, p3 ] =min(abs(t-newTR_time(itr)-4));
            [Yt, p4 ] =min(abs(t-newTR_time(itr)-7));
            
            EMG_rms(ichannel,itr) = rms(EMG_inter(ichannel,p1:p2))...
            ./rms(EMG_inter(ichannel,p3:p4));
            s=plot(t([p1,p2]),mean(EMG_inter(ichannel,:)).*[1 1]+(ichannel+1).*20,'g');
            set(s,'linewidth',2)
            
            s=plot(t([p3,p4]),mean(EMG_inter(ichannel,:)).*[1 1]+(ichannel+1).*20,'r');
            set(s,'linewidth',2)
            
        end
    end
    %     text(220,30,num2str(EMG_rms));
    
    figure(f2)
    subplot(2,3,ifinger)
    errorbar(mean(EMG_rms'),std(EMG_rms'));
    title(finger_names{ifinger})
    size(EMG_inter)
    %     sdf0
    EMG_rms
    file_save = ['Save_RMS_up\' Sub '_' num2str(Session) fingers(ifinger) '.mat'];
    save(file_save,'Session','ifinger','EMG_rms');
    file_save = ['Save_EMG_up\' Sub '_' num2str(Session) fingers(ifinger) '.mat'];
    save(file_save,'Session','ifinger','EMG8','newTR_time');
    % stop
    clear EMG8_down t EMG_inter EMG_new EMG_rms
    pause
end